package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.SOrder;
import com.example.mapper.SOrderMapper;
import com.example.service.SOrderService;
import com.example.service.SReportService;
import com.example.vo.SOrderDailyCountVO;
import com.example.vo.SReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SReportServiceImpl implements SReportService {
    @Autowired
    SOrderService sOrderService;
    @Autowired
    SOrderMapper sOrderMapper;
    @Override
    public List<SReportVO> getSales() {
        //把已收货的商品进行统计，统计他们的一个月的销量
        LambdaQueryWrapper<SOrder> eq = new QueryWrapper<SOrder>().lambda()
                .eq(SOrder::getState, 4)
                .or()
                .eq(SOrder::getState, 5);
        List<SOrder> sOrders = sOrderMapper.selectList(eq);
        List<SReportVO> list = new ArrayList<>();

        double sumSale = 0;
        for (SOrder sOrder : sOrders) {
            sumSale = sumSale +sOrder.getCount();
        }
        for (SOrder sOrder : sOrders) {
            SReportVO sReportVO = new SReportVO();
            sReportVO.setShoeName(sOrder.getSpuName());
            sReportVO.setShoeSales(sOrder.getCount().toString());
            Double percent =  ((double)sOrder.getCount()/sumSale)*100;
            sReportVO.setShoeSalesPercent(percent.toString());
            list.add(sReportVO);
        }
        return list;
    }

    @Override
    public List<SOrderDailyCountVO> getDailyCount() {
        List<SOrderDailyCountVO> dailyCount = sOrderMapper.getDailyCount();
        return dailyCount;
    }
}
